package com.suning.sawp.service.listener;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.suning.rsc.dto.responsedto.MbfServiceResponse;
import com.suning.rsc.mqservice.ei.annotation.EsbEIServerService;
import com.suning.rsc.server.NullResponseDto;
import com.suning.rsc.server.mq.MQServerServiceHandler;
import com.suning.sawp.dto.common.NsfNullReponseInterfaceForEsb;
import com.suning.sawp.intf.commodity.BppCommodityService;
import com.suning.sawp.po.commodity.BppCommodityInfo;
import com.suning.sawp.po.commodity.BppMqCommodity;
import com.suning.sawp.po.commodity.ReceiveBppGoodsInfo;
import com.suning.sawp.service.util.SCMConfigUtil;

/**
 * 接收大聚惠推送大聚惠商品的信息<br>
 * 〈功能详细描述〉
 * NmpsPreferenceMgmt-syncGrppuridPush
 * @author 13071470
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@EsbEIServerService(connectionFactory = "snsawpConnectionFactory", receiveQueue = "bppCommodityRespMDPQueue", consumers = 1)
public class ReceiveBppInfoListener
        extends MQServerServiceHandler<ReceiveBppGoodsInfo, NullResponseDto, NsfNullReponseInterfaceForEsb> {

    private static final String SERVICE_TYPE_5 = "5";// 渠道专享
    private static final Logger LOGGER = LoggerFactory.getLogger(ReceiveBppInfoListener.class);

    /**
     * 大聚会商品服务
     */
    @Resource
    BppCommodityService bppCommodityService;

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param list
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void handleCommodityList(List<BppMqCommodity> list) {
        LOGGER.info("ReceiveBppInf list={}", list);
        // 取SCM配置文件
        String categCodes = SCMConfigUtil.getConfig("bppCommodity.receiveCategCodes");
        LOGGER.debug("ReceiveBppInf categCodes={}", categCodes);
        List<String> categCodeList = new ArrayList<String>();
        if (StringUtils.isBlank(categCodes)) {
            categCodes = "246;368;584;590;591;437";
        }
        for (String categ : categCodes.split(";")) {
            categCodeList.add(categ);
        }
        LOGGER.info("ReceiveBppInf categCodeList={}", categCodeList);
        if (CollectionUtils.isNotEmpty(list)) {
            LOGGER.info("ReceiveBppInf list size={}", list.size());
            for (BppMqCommodity commodity : list) {
                if (SERVICE_TYPE_5.equals(commodity.getServiceType())
                        && categCodeList.contains(commodity.getCategCode())) {
                    handleCommodity(commodity);
                }
            }
        }
    }

    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param commodity
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void handleCommodity(BppMqCommodity commodity) {
        // published 1：手工下架 2：数量卖完下架 0：上架
        if ("0".equals(commodity.getPublished())) {
            saveBppCommodityInfo(commodity);
        } else {
            deleteBppCommodityInfo(commodity.getActivityId());
        }
    }

    /**
     * 
     * 功能描述:保存商品信息 <br>
     * 〈功能详细描述〉
     *
     * @param financeId
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void saveBppCommodityInfo(BppMqCommodity commodity) {
        LOGGER.debug("ReceiveBppInf saveBppCommodityInfo={}", commodity);
        Long id = bppCommodityService.judgeIsExsit(commodity.getActivityId());
        LOGGER.info("ReceiveBppInf old id={}", id);
        BppCommodityInfo info = new BppCommodityInfo();
        info.setActivityId(commodity.getActivityId());
        info.setCommCode(commodity.getCommCode());
        info.setMertCode(commodity.getMertCode());
        info.setPartName(commodity.getPartName());
        info.setPayPrice(commodity.getPayPrice());
        info.setGbPrice(commodity.getGbPrice());
        info.setCategCode(commodity.getCategCode());
        info.setGbBeginDate(commodity.getGbBeginDate());
        info.setGbEndDate(commodity.getGbEndDate());
        if (id == null) {
            info.setCreateTime(new Date());
            bppCommodityService.save(info);
        } else {
            info.setId(id);
            info.setCreateTime(new Date());
            bppCommodityService.update(info);
        }
    }

    /**
     * 
     * 功能描述:删除码上省商品 <br>
     * 〈功能详细描述〉
     *
     * @param commodity
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private void deleteBppCommodityInfo(String activityId) {
        LOGGER.info("deleteBppCommodityInfo activityId={}", activityId);
        if (StringUtils.isNotEmpty(activityId)) {
            bppCommodityService.delete(activityId);
        }
    }

    /**
     * 接受处理消息
     */
    @Override
    public NullResponseDto handleBizz(ReceiveBppGoodsInfo bizzRequestObject, String uid) {
        LOGGER.info("ReceiveBppGoodsInfo uid={}", uid);
        handleCommodityList(bizzRequestObject.getData());
        return null;
    }

    /**
     * 返回空
     */
    @Override
    public void sendResponse(NsfNullReponseInterfaceForEsb clientInterface, NullResponseDto response,
            MbfServiceResponse serviceResponse) {
        LOGGER.debug("no response");
    }

}
